package com.yupi.project.controller;

import com.yupi.yuapicommon.model.common.BaseResponse;
import com.yupi.yuapicommon.model.common.BusinessException;
import com.yupi.yuapicommon.model.common.ErrorCode;
import com.yupi.yuapicommon.model.common.ResultUtils;
import com.yupi.yuapicommon.utils.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/tool")
@Api("工具栏服务相关")
public class ToolBarController {

    @PostMapping("/file/2base64")
    @ApiOperation("文件转base64字符串")
    public BaseResponse<String> deleteInterfaceInfo(@RequestPart("file") MultipartFile multipartFile, HttpServletRequest request) {
        String base64string = null;
        try {
            base64string = ExcelUtils.fileToBase64string(multipartFile.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if(base64string == null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "文件转base64字符串失败");
        }
        return ResultUtils.success(base64string);
    }
}
